//
//  TKTieziDetailViewController.m
//  Ticket
//
//  Created by 小禹宝宝 on 2017/10/23.
//  Copyright © 2017年 cy55. All rights reserved.
//

#import "TKTieziDetailViewController.h"
#import "TKmainDetailHeaderView.h"
#import "TKMainGoodsDetailCell.h"
#import "TKMainGoodsDetailSectionView.h"
#import "TKMainDetailOrderCell.h"
#import "TKGoodsdetailResponse.h"
#import "TKOrderListResquest.h"
#import "BLBaseListViewDataController.h"

@interface TKTieziDetailViewController ()
@property (nonatomic, strong) TKmainDetailHeaderView *topView;
@property (nonatomic, strong) TKGoodsInfo *goodsInfo;
@property (nonatomic, assign) NSInteger sectionCount;
@property (nonatomic, strong) BLBaseListViewDataController *dataSource;


@end

@implementation TKTieziDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"详情";
    [self ks_setupLeftBackBarButtonItemWithColor:nil];
//    self.tableView.rowHeight = UITableViewAutomaticDimension;
//    self.tableView.estimatedRowHeight = 135;
    
    [self createTableHeaderView];
    [self.listView registerNib:[UINib nibWithNibName:@"TKMainGoodsDetailCell" bundle:nil] forCellReuseIdentifier:@"TKMainGoodsDetailCell"];
    [self.listView registerNib:[UINib nibWithNibName:@"TKMainDetailOrderCell" bundle:nil] forCellReuseIdentifier:@"TKMainDetailOrderCell"];
    [self prepareDataSource];
}

- (void)prepareDataSource {
    NSDictionary *params = @{@"goods_id":self.goodsID};
    @weakify(self)
    [BLApi GET:URL_GoodsDetail_info parameters:params decodeClass:[TKGoodsdetailResponse class] completion:^(id responseObject, id responseDataOriginal, NSError *error) {
        if (!error) {
            @strongify(self)
            TKGoodsdetailResponse *response = responseObject;
            if ([response isSuccess]) {
                self.goodsInfo = response.info;
                [self refreshHeaderView];
                if (self.goodsInfo.sell_type == 2) {
                    //拍卖时展示拍单用户
                    self.sectionCount = 2;
                    //请求该商品拍单列表数据
                    TKOrderListResquest *request = [TKOrderListResquest new];
                    if ([self.goodsInfo.id jc_isValidString]) {
                        request.goods_id = self.goodsInfo.id;
                    }
                    
                    _dataSource = [[BLBaseListViewDataController alloc] initWithRequest:request decodeClass:[TKOrderListResponse class] afterParseCompletion:^(id responseObject) {
                        DDLogInfo(@"responseObject ----:%@",responseObject);
                        
                        DDLogInfo(@"responseObject ---- list:%@",[(TKOrderListResponse *)responseObject list]);
                    }];
                    self.listView.pagingListDataSource = _dataSource;

                    
                } else {
                    self.sectionCount = 1;
                }
                [self.listView reloadData];
            }
        }
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (![SystemHelper checkAndShowLoginViewControllerIfNotLogin]) {
        [self ks_showToast:@"您必须登录后才能购买商品"];
    }
}



- (void)createTableHeaderView {
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"TKmainDetailHeaderView" owner:nil options:nil] firstObject];
    self.listView.tableHeaderView = self.topView;
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.listView);
        make.height.mas_equalTo(390);
    }];

}

- (void)refreshHeaderView {
    self.topView.model = self.goodsInfo;
}
#pragma tableView--delegate


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return 70;
    }
    
    self.listView.rowHeight = UITableViewAutomaticDimension;
    self.listView.estimatedRowHeight = 135;
    return self.listView.rowHeight;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionCount;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    else {
        return self.listView.dataItems.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        TKMainGoodsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TKMainGoodsDetailCell" forIndexPath:indexPath];
        cell.model = self.goodsInfo;
        return cell;
    } else if (indexPath.section == 1) {
        TKMainDetailOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TKMainDetailOrderCell" forIndexPath:indexPath];
        TKOrderItem *item = self.listView.dataItems[indexPath.row];
        cell.model = item;

        
        return cell;
    }
        return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 61;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    TKMainGoodsDetailSectionView *headerSectionView = [[[NSBundle mainBundle] loadNibNamed:@"TKMainGoodsDetailSectionView" owner:nil options:nil] firstObject];
    switch (section) {
        case 0:
            headerSectionView.titleLabel.text = @"商品详情";
            break;
        case 1:
            headerSectionView.titleLabel.text = @"拍单用户";
            break;
        case 2:
            headerSectionView.titleLabel.text = @"评论";
            break;
        default:
            break;
    }
    return headerSectionView;
}
@end
